from google.appengine.ext import db
from models.gamebase import *
import time


def timestamp():
    return str(long(time.time()*1000))

class GameLogEntryBase(GamePolyModel):
    game= db.ReferenceProperty(None, required=True, collection_name="gamelogentries")
    timestamp=db.IntegerProperty(required=True)
    type = db.StringProperty(required=True)
    command=db.StringProperty(required=True) 

    def to_dictionary(self):
        dct={}
        for name in self._properties.keys():
            value=self._properties[name]
            if isinstance(value, db.Property) and name != "game" and name[0] != "_" and getattr(self, name)!=None:
                if (name=="timestamp"):
                    dct[name]=str(getattr(self, name))
                else:
                    dct[name]=getattr(self, name)
                #dct[name]=value if not hasattr(value,"__len__") or len(value) > 0 else None  
        return dct
    
class GameFlowLogEntry(GameLogEntryBase):
    level=db.StringProperty(default="info")
    message=db.StringProperty(required=True)
    
class DebugLogEntryBase(GameLogEntryBase):
    level = db.StringProperty(default="debug")
    
class ListChangeLogEntry(DebugLogEntryBase):
    add=db.StringListProperty(default=[])
    remove=db.StringListProperty(default=[])

class PlayerListChangeLogEntry(ListChangeLogEntry):
    position=db.IntegerProperty(required=True)
    
class StringPropertyChangeLogEntry(DebugLogEntryBase):
    property = db.StringProperty(required=True)
    old_value=db.StringProperty(default=None)
    new_value=db.StringProperty(default=None)

class IntegerPropertyChangeLogEntry(DebugLogEntryBase):
    property = db.StringProperty(required=True)
    old_value=db.IntegerProperty(default=None)
    new_value=db.IntegerProperty(default=None)

class PlayerGrowthLogEntry(DebugLogEntryBase):
    position=db.IntegerProperty(required=True)
    growth=db.IntegerProperty(required=True)
    
class BuildingStockChangeLogEntry(ListChangeLogEntry):
    pass

class PlayerBuildingChangeLogEntry(DebugLogEntryBase):
    position=db.IntegerProperty(required=True)
    occupy=db.StringListProperty(default=[])
    pay=db.StringListProperty(default=[])
    add=db.StringProperty(default=None)
    
class PlayerTokenChangeLogEntry(PlayerListChangeLogEntry):
    pass

class ShippingTrackChangeLogEntry(DebugLogEntryBase):
    region=db.IntegerProperty(required=True)
    player=db.IntegerProperty(required=True)
    token=db.StringProperty(default=None)

class CityChangeLogEntry(DebugLogEntryBase):
    city=db.IntegerProperty(required=True)
    occupy=db.IntegerProperty(default=None)
    take=db.StringProperty(default=None)
    attack=db.IntegerProperty(default=None)

class ConnectionChangeLogEntry(DebugLogEntryBase):
    a_end=db.IntegerProperty(required=True)
    b_end=db.IntegerProperty(required=True)
    occupy=db.IntegerProperty(default=None)
    displace=db.IntegerProperty(default=None)
    take=db.StringProperty(default=None)

class CardPileChangeLogEntry(DebugLogEntryBase):
    region=db.IntegerProperty(default=None)
    use_slavery_pile=db.BooleanProperty(default=False)
    position=db.IntegerProperty(required=True)
    add=db.StringProperty(default=None)
    remove=db.StringProperty(default=None)

class AbolishSlaveryLogEntry(DebugLogEntryBase):
    pass

class PlayerDiscardRequiredLogEntry(DebugLogEntryBase):
    position=db.IntegerProperty(required=True)
    
class PlayerDiscardLogEntry(DebugLogEntryBase):
    position=db.IntegerProperty(required=True)
    discard=db.StringListProperty(default=[])
    pass_player=db.BooleanProperty(default=False)
        
class PlayerPassLogEntry(DebugLogEntryBase):
    position=db.IntegerProperty(required=True)
    auto_pass=db.BooleanProperty(default=False)
    phase=db.StringProperty(required=True)
    discard_required=db.BooleanProperty(default=False)

class CurrentPlayerLogEntry(DebugLogEntryBase):
    position=db.IntegerProperty(required=True)
    player_changed=db.BooleanProperty(default=True)

class NextRoundLogEntry(DebugLogEntryBase):
    round=db.IntegerProperty(required=True)
    start_player=db.IntegerProperty(required=True)

class NewPhaseLogEntry(DebugLogEntryBase):
    phase=db.StringProperty()
## Lobby log entries

class GameStartLogEntry(DebugLogEntryBase):
    pass

class PlayerPhaseStartLogEntry(DebugLogEntryBase):
    position=db.IntegerProperty(required=True)
    round=db.IntegerProperty(required=True)
    phase=db.StringProperty(required=True)
    
class PlayerReadyLogEntry(DebugLogEntryBase):
    position=db.IntegerProperty(required=True)
    is_ready=db.BooleanProperty(required=True)

class PlayerColourLogEntry(DebugLogEntryBase):
    position=db.IntegerProperty(required=True)
    colour=db.StringProperty(required=True)
    
class PlayerLeaveLogEntry(DebugLogEntryBase):
    position=db.IntegerProperty(required=True)
    new_owner_position=db.IntegerProperty(default=None)

class PlayerJoinLogEntry(DebugLogEntryBase):
    position=db.IntegerProperty(required=True)
    email=db.StringProperty(required=True)
    nickname=db.StringProperty(required=True)
    name=db.StringProperty(required=True)
    colour=db.StringProperty(required=True)
    
